package com.neil.XXnearby;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PoilistActivity extends Activity {
    private ListView listView;
    private List<Map<String,Object>> data = new ArrayList<Map<String, Object>>();
    private List<String>messagenamedata = new ArrayList<String>();
    private List<String>messagedetaildata = new ArrayList<String>();
    private List<String>messagedistancedata = new ArrayList<String>();
    private View distanceselect;
    private  SimpleAdapter  simpleAdapter;
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.poidetail);
        listView = (ListView) findViewById(R.id.firstitem);
        listView.setDivider(new ColorDrawable(Color.BLACK));
        listView.setDividerHeight(1);
        distanceselect = findViewById(R.id.distanceselected);
        init();
    }

    private void init() {
        inittitle();
        initdistancebar();
        initlistView();

    }

    private void initdistancebar() {
        distanceselect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(PoilistActivity.this);
                LayoutInflater inflater = LayoutInflater.from(PoilistActivity.this);
                View view1 = inflater.inflate(R.layout.distance, null);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.FILL_PARENT,
                        LinearLayout.LayoutParams.FILL_PARENT);
                view1.setLayoutParams(params);


                builder.setView(view1);

                final AlertDialog loginDialog = builder.create();
                loginDialog.show();
                TextView button1 = (TextView) view1.findViewById(R.id.button1);

                TextView button2 = (TextView) view1.findViewById(R.id.button2);

                TextView button3 = (TextView) view1.findViewById(R.id.button3);

                TextView button4 = (TextView) view1.findViewById(R.id.button4);

                TextView button5 = (TextView) view1.findViewById(R.id.button5);

                button1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        TextView textView = (TextView) distanceselect.findViewById(R.id.distancemessage);
                        textView.setText("范围:1000m内");
                        loginDialog.dismiss();

                    }
                });
                button2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        TextView textView = (TextView) distanceselect.findViewById(R.id.distancemessage);
                        textView.setText("范围:2000m内");
                        loginDialog.dismiss();
                    }
                });
                button3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        TextView textView = (TextView) distanceselect.findViewById(R.id.distancemessage);
                        textView.setText("范围:3000m内");
                        loginDialog.dismiss();
                    }
                });
                button4.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        TextView textView = (TextView) distanceselect.findViewById(R.id.distancemessage);
                        textView.setText("范围:4000m内");
                        loginDialog.dismiss();
                    }
                });
                button5.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        TextView textView = (TextView) distanceselect.findViewById(R.id.distancemessage);
                        textView.setText("范围:5000m内");
                        loginDialog.dismiss();
                    }
                });
           }
        });
    }

    private void inittitle() {
        TextView textView = (TextView) findViewById(R.id.titletextview);
        textView.setText(getIntent().getCharSequenceExtra("title"));
        ImageButton backbutton = (ImageButton) findViewById(R.id.titlebackbutton);
        backbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }


    private void initlistView(){
        initmessagedata();
        for(int x=0;x<messagenamedata.size();x++){
            HashMap<String,Object> hashMap = new HashMap<String, Object>();
            hashMap.put("name",messagenamedata.get(x));
            hashMap.put("detail",messagedetaildata.get(x));
            hashMap.put("distance",messagedistancedata.get(x));
            data.add(hashMap);
        }

        simpleAdapter = new SimpleAdapter(this,data,R.layout.poilistitem,new String[]{"name","detail","distance"},new int[]{R.id.name,R.id.detail,R.id.distance}){
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view =super.getView(position, convertView, parent);




                return view;    //To change body of overridden methods use File | Settings | File Templates.
            }
        };
        TextView textView1 = new TextView(this);
        textView1.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        textView1.setGravity(View.TEXT_ALIGNMENT_GRAVITY);
        textView1.setPadding(0, 3, 0, 3);
        textView1.setText("加载更多");


        listView.addFooterView(textView1);

        listView.setAdapter(simpleAdapter);
        textView1.setOnClickListener(new View.OnClickListener() {
            private TextView textView;
            @Override
            public void onClick(View view) {
                textView = (TextView) view;
                textView.setText("加载中...");
                AsyncTask<Object,Object,Object> asyncTask = new AsyncTask<Object, Object, Object>() {
                    @Override
                    protected Object doInBackground(Object... objects) {


                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                        }
                        for(int x=0;x<5;x++){
                            HashMap<String,Object> hashMap = new HashMap<String, Object>();
                            hashMap.put("name","陕西大面馆");
                            hashMap.put("detail","西安南大街");
                            hashMap.put("distance","1200m");
                            data.add(hashMap);
                        }
                        return null;  //To change body of implemented methods use File | Settings | File Templates.
                    }

                    @Override
                    protected void onPostExecute(Object o) {
                        super.onPostExecute(o);    //To change body of overridden methods use File | Settings | File Templates.
                        simpleAdapter.notifyDataSetChanged();
                        textView.setText("加载更多");
                    }


                };

                asyncTask.execute();

            }
        });
        //   listView.add
    }

    private void initmessagedata(){
        messagenamedata.add("黄记煌三汁闷锅");
        messagedetaildata.add("西安南大街53号南附楼内3层");
        messagedistancedata.add("500m");
        messagenamedata.add("香港私家菜天子海鲜火锅");
        messagedetaildata.add("西安南大街");
        messagedistancedata.add("800m");
        messagenamedata.add("黄记煌三汁闷锅");
        messagedetaildata.add("西安南大街53号南附楼内3层");
        messagedistancedata.add("500m");
        messagenamedata.add("香港私家菜天子海鲜火锅");
        messagedetaildata.add("西安南大街");
        messagedistancedata.add("800m");
        messagenamedata.add("黄记煌三汁闷锅");
        messagedetaildata.add("西安南大街53号南附楼内3层");
        messagedistancedata.add("500m");
        messagenamedata.add("香港私家菜天子海鲜火锅");
        messagedetaildata.add("西安南大街");
        messagedistancedata.add("800m");
        messagenamedata.add("黄记煌三汁闷锅");
        messagedetaildata.add("西安南大街53号南附楼内3层");
        messagedistancedata.add("500m");
        messagenamedata.add("香港私家菜天子海鲜火锅");
        messagedetaildata.add("西安南大街");
        messagedistancedata.add("800m");
        messagenamedata.add("黄记煌三汁闷锅");
        messagedetaildata.add("西安南大街53号南附楼内3层");
        messagedistancedata.add("500m");
        messagenamedata.add("香港私家菜天子海鲜火锅");
        messagedetaildata.add("西安南大街");
        messagedistancedata.add("800m");
        messagenamedata.add("黄记煌三汁闷锅");
        messagedetaildata.add("西安南大街53号南附楼内3层");
        messagedistancedata.add("500m");
        messagenamedata.add("香港私家菜天子海鲜火锅");
        messagedetaildata.add("西安南大街");
        messagedistancedata.add("800m");

    }

}
